package com.fumin.controller;

import com.fumin.beans.PageQuery;
import com.fumin.common.JsonData;
import com.fumin.model.SysRole;
import com.fumin.param.AclParam;
import com.fumin.service.SysAclService;
import com.fumin.service.SysRoleService;
import com.fumin.service.SysTreeService;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 权限点controller
 * @Author xiayuxuanmin
 * @Date 2019/11/8
 * @Description
 */
@Controller
@RequestMapping("/sys/acl")
@Slf4j
public class SysAclController {

    @Resource
    private SysAclService sysAclService;
    @Resource
    private SysTreeService sysTreeService;
    @Resource
    private SysRoleService sysRoleService;

    //项目默认以JSON返回的，用.json结尾，返回页面的以.page结尾
    @RequestMapping("/acl.page")
    public ModelAndView page(){
        //在视图解析器中，根据prefix和suffix查找对应的JSP文件，这里是查找/WEB-INF下面的/views/acl.jsp文件
        return new ModelAndView("acl");
    }

    @RequestMapping("/save.json")
    @ResponseBody
    public JsonData saveAcl(AclParam param){
        sysAclService.save(param);
        return JsonData.success();
    }

    @RequestMapping("/page.json")
    @ResponseBody
    public JsonData list(@RequestParam("aclModuleId") Integer aclModuleId, PageQuery pageQuery){
        return JsonData.success(sysAclService.getPageByAclModuleId(aclModuleId,pageQuery));
    }

    @RequestMapping("/update.json")
    @ResponseBody
    public JsonData updateAcl(AclParam param){
        sysAclService.update(param);
        return JsonData.success();
    }

    @RequestMapping("acls.json")
    @ResponseBody
    public JsonData acls(@RequestParam("aclId") int aclId){
        Map<String,Object> map = Maps.newHashMap();
        List<SysRole> roleList = sysRoleService.getRoleListByAclId(aclId);
        map.put("roles",roleList);//角色已经分配过去的所有角色
        map.put("users",sysRoleService.getUserListByRoleList(roleList));//角色已经分配过去的所有用户
        return JsonData.success(map);
    }
}
